package com.csx.governance.traffic.core.context;

/**
 * <p>{@link TrafficContext} 上下文对象的持有器</p>
 * <p>{@link TrafficContext} 主要负责保存 入栈 和 出栈 的请求对象</p>
 * @author cuisongxu
 * @date 2023/3/18 19:10
 */
public class TrafficContextHolder {

    private static final TrafficContextHolder INSTANCE = new TrafficContextHolder();

    private final ThreadLocal<TrafficContext> contextThreadLocal = new ThreadLocal<>();

    private TrafficContextHolder() {}


    public static TrafficContext getContext() {
        return INSTANCE.contextThreadLocal.get();
    }

    public static TrafficContextHolder setContext(TrafficContext context) {
        INSTANCE.contextThreadLocal.set(context);
        return INSTANCE;
    }

    public static TrafficContextHolder clearContext() {
        INSTANCE.contextThreadLocal.remove();
        return INSTANCE;
    }
}
